//
//  LCjackTbCell.m
//  TheLCNews
//
//  Created by apple on 15/12/1.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCjackTbCell.h"

@implementation LCjackTbCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.backView = [[UIView alloc] init];
//    self.backView.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.backView];
    
    _backView.layer.shadowOffset = CGSizeMake(0, 0);
    _backView.layer.shadowColor = [UIColor blackColor].CGColor;
    _backView.layer.shadowOpacity = 0.8;
    _backView.layer.shadowRadius = 1;
    
    self.thumbnailImageView = [[UIImageView alloc] init];
    [self.backView addSubview:self.thumbnailImageView];
//    self.thumbnailImageView.backgroundColor = [UIColor yellowColor];
    
    _thumbnailImageView.layer.shadowOffset = CGSizeMake(0, 0);
    _thumbnailImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    _thumbnailImageView.layer.shadowOpacity = 0.8;
    _thumbnailImageView.layer.shadowRadius = 1;
    
    
    // 显示主要内容Label
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont systemFontOfSize:17.];
    self.contentLabel.numberOfLines = 0;
    // 可以自动适应文本内容
    [self.contentLabel sizeToFit];
    [self.backView addSubview:self.contentLabel];
    
    
    // "赞"的显示次数label
    self.likesLabel = [[UILabel alloc] init];
    [self.backView addSubview:self.likesLabel];
   
    self.likesLabel.textAlignment = NSTextAlignmentCenter;
    _likesLabel.font = [UIFont systemFontOfSize:14.];
    _likesLabel.textColor = [UIColor lightGrayColor];
    
    // 显示评论次数的Label
    self.commentsLabel = [[UILabel alloc] init];
    [self.backView addSubview:self.commentsLabel];
   
    self.commentsLabel.textAlignment = NSTextAlignmentCenter;
    _commentsLabel.font = [UIFont systemFontOfSize:14.];
    _commentsLabel.textColor = [UIColor lightGrayColor];
}
#pragma mark button的点击方法




- (void)layoutSubviews{
    [super layoutSubviews];
    // 最后运行的一个方法,在这个方法里,属性已经被赋好值了
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.], NSFontAttributeName, nil];
    // 根据文本字体大小计算文本的cell
    // 第一个参数:文字显示的最大尺寸范围,最大(375, 0) 0代表最大的意思
    CGRect rect = [self.contentLabel.text boundingRectWithSize:CGSizeMake(355, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 文字的frame
    self.contentLabel.frame = CGRectMake(KWIDTH / 37.5, KHEIGHT / (KHEIGHT/ ([self.height floatValue] + KHEIGHT / 44.46667)), KWIDTH / 1.05633, rect.size.height);
    
    // 图片自适应的frame
    self.thumbnailImageView.frame = CGRectMake(KWIDTH / 37.5, KHEIGHT / 66.7, KWIDTH / 1.05633, KHEIGHT / (KHEIGHT / [self.height floatValue]));
    
    
    
    // 通过判断设置背景View的frame,当文字为空的时加长View的背景
    if (self.contentLabel.text.length != 0) {
        // 背景Viewframe
        self.backView.frame = CGRectMake(0, KHEIGHT / 133.4, KWIDTH, [self.height floatValue] + rect.size.height + KHEIGHT / 12.12727);
        // "赞"次数的显示
        self.likesLabel.frame = CGRectMake(KWIDTH / 37.5, KHEIGHT / (KHEIGHT / ([self.height floatValue] + rect.size.height + KHEIGHT / 33.35)), KWIDTH / 6.25, KHEIGHT / 22.2333);
        // 显示评论次数的label
        self.commentsLabel.frame = CGRectMake(KWIDTH / 1.25, KHEIGHT / (KHEIGHT / ([self.height floatValue] + rect.size.height + KHEIGHT / 33.35)), KWIDTH / 7.5, KHEIGHT / 22.2333);
    }
    else if (self.contentLabel.text.length == 0){
        // 背景Viewframe
        self.backView.frame = CGRectMake(0, KHEIGHT / 133.4, KWIDTH, (KHEIGHT / (KHEIGHT / ([self.height floatValue] + KHEIGHT / 13.34))));
        // "赞"次数的显示
        self.likesLabel.frame = CGRectMake(KWIDTH / 37.5, KHEIGHT / (KHEIGHT / ([self.height floatValue] + KHEIGHT / 44.467)), KHEIGHT / 6.25, KHEIGHT / 22.2333);
        // 显示评论次数的label
        self.commentsLabel.frame = CGRectMake(KWIDTH / 1.25, KHEIGHT / (KHEIGHT / ([self.height floatValue] + KHEIGHT / 44.467)), KWIDTH / 7.5, KHEIGHT / 22.2333);
        // 文字frame
        self.contentLabel.frame = CGRectNull;           // 设置无frame
    }
    
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
